package com.shop.dao.impl;

import com.shop.dao.IProductDao;
import com.shop.entity.Product;
import com.shop.util.JDBCUtils;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.BeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductDaoImpl extends BaseDao implements IProductDao {

    private QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
    private static Map<String,String> map = new HashMap<>();

    static {
        map.put("p_id","pid");
        map.put("t_id","tid");
        map.put("p_name","pname");
        map.put("p_time","ptime");
        map.put("p_image","pimage");
        map.put("p_price","pprice");
        map.put("p_state","pstate");
        map.put("p_info","pinfo");
    }

    /**
     * 通过pid查询商品所有信息
     * @param pid
     * @return
     */
    @Override
    public Product findProductByPid(int pid) {
        String sql = "select p_id,t_id,p_name,p_time,p_image,p_price,p_state,p_info from product where p_id = ?";
        return queryForOne(Product.class,sql,map,pid);
    }

    //查询所有
    @Override
    public List<Product> list(int tid) {
        String sql = "select * from product where t_id =?";
        try {
            List<Product> query = queryRunner.query(sql, new BeanListHandler<>(Product.class, new BasicRowProcessor(new BeanProcessor(map))),tid);
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
    //分页查询
    @Override
    public List<Product> list(int tid, int begin, int finish) {
        String sql = "SELECT * FROM product WHERE t_id = ? limit ?,?";
        try {
            List<Product> query = queryRunner.query(sql,
                    new BeanListHandler<>(Product.class,
                            new BasicRowProcessor(new BeanProcessor(map)))
                    ,tid,((begin - 1) * finish),finish);
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;


    }
    //模糊分页
    @Override
    public List<Product> list(String pname, int begin, int finish) {
        String sql = "SELECT * FROM product WHERE p_name LIKE ? LIMIT ?,?";
        try {
            List<Product> query = queryRunner.query(sql,
                    new BeanListHandler<>(Product.class,
                            new BasicRowProcessor(new BeanProcessor(map)))
                    ,"%"+pname+"%",((begin - 1) * finish),finish);
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    //查询个数
    @Override
    public Long count(int tid) {
        String sql="select count(*) from product WHERE t_id = ?";
        try {
            Long result = queryRunner.query(sql, new ScalarHandler<>(),tid);
            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    //使用关键字获取总个数
    @Override
    public Long count(String pname) {
        String sql="select count(*) from product WHERE p_name LIKE ?";
        try {
            Long result = queryRunner.query(sql, new ScalarHandler<>(),"%"+pname+"%");
            return result;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    //查询一个
    @Override
    public Product listone(int pid) {
        String sql="select * from product WHERE p_id =?";
        try {
            Product query = queryRunner.query(sql, new BeanHandler<>(Product.class, new BasicRowProcessor(new BeanProcessor(map))), pid);

            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
    //模糊查询
    @Override
    public List<Product> like(String pname) {
        String sql="SELECT * FROM product WHERE p_name LIKE ?";
        try {
            List<Product> query = queryRunner.query(sql, new BeanListHandler<>(Product.class, new BasicRowProcessor(new BeanProcessor(map))), "%"+pname+"%");
            return query;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }


}
